<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Admin extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}
	public function index($page = 0)
	{
		$page = 'admin/home_view';
		$data = array(
					'title' => 'Admin Home Page',
					'nav' => __FUNCTION__
			);
		
		$this->buildPage($page, $data);
	}
	public function vendor($offset = 0){
		
		$limit = 20;
		$page = 'admin/vendor_view';
		$data = array(
				'title' => 'Admin Vendor Page',
				'nav' => __FUNCTION__
		);
		$this->load->model('vendor_model');
		$data['fields'] = $this->vendor_model->getPublicSearchField();
		
		list($vendors, $totalResult) = $this->vendor_model->search(array(), $offset, $limit);
		$data['vendors'] = $vendors;
		$data['totalResult'] = $totalResult;

		//pagination
		$this->load->library('pagination');
		$config['base_url'] = site_url('admin/vendor');
		$config['total_rows'] = $data['totalResult'];
		$config['per_page'] = $limit;
		$config['num_links'] = 10;
		$config['url_segment'] = 3;
		
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		//TODO not done, still need to include search
		
		$this->buildPage($page, $data);
	}
	public function vendorDetail(){
		$data = array(
				'title' => 'Admin Vendor Page',
				'nav' => 'vendor'
		);
		$page = 'admin/vendor_detail_view';
		
		$vendor_id = $this->uri->segment(3, 0);
		$this->load->model('vendor_model');
		try {
			$vendor = $this->vendor_model->loadVendor($vendor_id);
			$data['vendor'] = $vendor;
			$data['primary_contact'] =  $vendor->getPrimaryContact();
			
		} catch (Object_not_found_exception $e) {
			//TODO: what should i do??
		}
		$data['branches'] = $vendor->getBranches();
		$this->buildPage($page, $data);
	}
	public function addVendor(){
		$data = array(
				'title' => 'Add Vendor Page',
				'nav' => 'vendor'
		);
		$page = 'admin/add_vendor_step1_view';
	
		$this->buildPage($page, $data);
	}
	public function createVendor(){
		$vals = $this->input->post();
		$this->load->model('vendor_model');
		$id = $this->vendor_model->createVendor($vals);
		redirect('/admin/addBranches/vendor/'.$id);
	}
	public function addBranches(){
		$data = array(
				'title' => 'Add Branch Page',
				'nav' => 'vendor'
		);
		$page = 'admin/add_vendor_step2_view';
		
		$vendor_id = $this->uri->segment(4, 0);
		$this->load->model('vendor_model');
		$vendor = $this->vendor_model->loadVendor($vendor_id);
		
		//$this->buildPage($page, $data);
	}
	public function logout()
	{
		$this->session->unset_userdata('logged_in');
		session_destroy();
		redirect('admin', 'refresh');
	}
	private function buildPage($page, $data){
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			$data['username'] = $session_data['username'];
			$this->template->set('nav_list', array('index'=> 'home','vendor'=> 'vendor', 'coupon' => 'coupon', 'logout'=>'logout'));
			$this->template->load('admin_template', $page, $data);
		}
		else
		{
			//If no session, redirect to login page
			redirect('login', 'refresh');
		}
	}
}